package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.ZrReportDao;
import com.hhs.storage.service.BaseDepartmentService;
import com.hhs.storage.service.BasePermissionService;
import com.hhs.storage.service.BaseUserService;
import com.hhs.storage.service.ZrReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.DateEasy;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yuanjun
 * @since 2024-01-12
 */
@Service
public class ZrReportServiceImpl extends ServiceImpl<ZrReportDao, ZrReport> implements ZrReportService {
    @Resource
    BaseUserService baseUserService;
    @Resource
    HttpServletRequest request;
    @Resource
    ZrReportDao zrReportDao;
    @Resource
    BasePermissionService basePermissionService;
    @Resource
    BaseDepartmentService baseDepartmentService;

    @Override
    public Map<String, Object> getAll(ZrReport zrReport, InputParam inputParam) {
        inputParam.setCurUserId(SysUtil.getLoginUserId(request));
        //前台没有传部门Id默认登录者的部门与子部门
        if(zrReport.getDepartIds().length==0&&!inputParam.getCurUserId().equals("2")&&!inputParam.getCurUserId().equals("21")){
            Map<String, Object> resMap = baseDepartmentService.getUserDepartInfo();
            List<BaseDepartment> listd=(List)resMap.get("data");
            List<String> listIds=new ArrayList<>();
            for (BaseDepartment b :
                    listd) {
                listIds.add(b.getId());
            }
            String[] ids=new String[listIds.size()];
            listIds.toArray(ids);
            zrReport.setDepartIds(ids);
        }
        //得到登录用户的权限
        boolean hasp = basePermissionService.hasPermission(inputParam.getCurUserId(), "zjbgck");
        if (hasp) {
            inputParam.setPermission("zjbgck");
        }
        //得到数据
        List<ZrReport> list = zrReportDao.getAll(zrReport, inputParam);
        //得到total
        Integer total = zrReportDao.getTotal(zrReport, inputParam);
        for (ZrReport item :
                list) {
            item.setUser(baseUserService.getById(item.getUserId()));
            if(!StringUtil.iszNull(item.getDepartmentId())){
                item.setDepartmentName(baseDepartmentService.getById(item.getDepartmentId()).getName());
            }
        }
        Map<String, Object> rmap = new HashMap<>();
        rmap.put("code", 1);
        rmap.put("rows", list);
        rmap.put("total", total);

        return rmap;
    }

    @Override
    public Map<String, Object> getCount(ZrReport zrReport, InputParam inputParam) {
        inputParam.setCurUserId(SysUtil.getLoginUserId(request));
        //得到登录用户的权限
        List<BasePermission> permissionList = basePermissionService.getByUserId(SysUtil.getLoginUserId(request));
        for (BasePermission item :
                permissionList) {
            if ("zjbgck".equals(item.getFlag())) {
                inputParam.setPermission("zjbgck");
                break;
            }
        }
        //0日，1周，2月，3季，4年
        Integer c0 = zrReportDao.getCount(zrReport, inputParam, "0");
        Integer c1 = zrReportDao.getCount(zrReport, inputParam, "1");
        Integer c2 = zrReportDao.getCount(zrReport, inputParam, "2");
        Integer c3 = zrReportDao.getCount(zrReport, inputParam, "3");
        Integer c4 = zrReportDao.getCount(zrReport, inputParam, "4");
        Map<String, Object> map = new HashMap<>();
        map.put("tCount", c0 + c1 + c2 + c3 + c4);//全部
        map.put("dCount", c0);//日
        map.put("wCount", c1);//周
        map.put("mCount", c2);//月
        map.put("sCount", c3);//季
        map.put("yCount", c4);//年
        return ResponseInfo.success(map);
    }

    @Override
    public ResponseBean<ZrReport> getFormPlan(InputParam inputParam) {
        QueryWrapper<ZrReport> qw=new QueryWrapper<>();
        if(!StringUtil.iszNull(inputParam.getId())){
            qw.eq("user_id",inputParam.getId());
        }
        String notDataMsg="";
        if(inputParam.getType()!=null){
            qw.eq("type",inputParam.getType());
            if(inputParam.getType()==0){    //日
                System.out.println(inputParam.getStartDate());
                DateEasy start = new DateEasy(inputParam.getStartDate());

                DateEasy end = new DateEasy(inputParam.getStartDate());
                end.addDate(1);
                System.out.println("查询参数"+start);
                System.out.println("查询参数"+end);
                qw.between("create_time",start.toLocalDate(),end.toLocalDate());
                notDataMsg="没有日报";
            }
            if(inputParam.getType()==1){    //周

                DateEasy start = DateEasy.getTimesWeekMorning(new DateEasy(inputParam.getStartDate()));
                DateEasy end = new DateEasy(start);
                end.addDate(7);
                qw.between("create_time",start.toLocalDate(),end.toLocalDate());
                notDataMsg="没有周报";

            }
            if(inputParam.getType()==2){    //月
                DateEasy start = DateEasy.getTimesMonthMorning(new DateEasy());
                DateEasy end = new DateEasy(start);
                end.addDate(start.getMaxDate());
                qw.between("create_time",start.toLocalDate(),end.toLocalDate());
                notDataMsg="没有月报";
            }
            if(inputParam.getType()==3){    //季
                DateEasy start = DateEasy.getTimesJdMorning(new DateEasy());
                DateEasy end = new DateEasy(start);
                end.addDate(start.getMaxDate()+start.addMonth(1).getMaxDate()+start.addMonth(1).getMaxDate());
                qw.between("create_time",start.toLocalDate(),end.toLocalDate());
                notDataMsg="没有季报";
            }
            if(inputParam.getType()==4){    //年
                DateEasy start = new DateEasy();
                try {
                    DateEasy s = new DateEasy(start.getYear() + "-01" + "-" + "01");
                    DateEasy e = new DateEasy(start.getYear() + 1 + "-01" + "-" + "01");
                    qw.between("create_time",s.toLocalDate(),e.addDate(-1).toLocalDate());
                }catch (Exception e){
                    e.printStackTrace();
                }
                notDataMsg="没有年报";
            }
        }
        qw.orderByDesc("create_time");
        List<ZrReport> list = this.list(qw);
        ResponseBean res=ResponseBean.getInstance();
        res.setCode(1);
        if(list.size()>0){
            res.setData(this.list(qw));
        }else{
            res.setCode(0);
            res.setMsg(notDataMsg);
        }
        System.out.println("----------------    "+res);
        return res;

    }
}
